package com.ruoyi.starbucks.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.autoconfigure.PageHelperProperties;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

@Slf4j
public class SrmService6 {
    public static String userName = "xbkww";

    public static String password = "xbkww";

    public static String token = "";

    public static void Login()
    {
        String url = MessageFormat.format("http://st.jiuapi.cn/api/home/user/login?username={0}&password={1}",userName,password);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        boolean result = jsonObject.getBoolean("state");
        if (result)
        {
            token = jsonObject.getString("message");
            log.info("登录,token="+token);
        }
    }

    public static String GetPhoneNo() {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url = MessageFormat.format("http://qh.jiuapi.cn/api/home/get/phone?token={0}",token);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        boolean result = jsonObject.getBoolean("state");
        if (result)
        {
            String phoneNo = jsonObject.getString("message");
            return phoneNo;
        }
        return "";
    }

    public static String GetMessage(String phoneNo) {
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url = MessageFormat.format("http://vd.jiuapi.cn/api/home/get/message?token={0}&phone={1}",token,phoneNo);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        boolean result = jsonObject.getBoolean("state");
        if (result)
        {
            String msg = jsonObject.getString("message");
            if(StringUtils.isNotEmpty(msg)){
                msg=MessageFormat.format("【星巴克中国】亲爱的用户，欢迎使用星巴克App，您的验证码为{0}（5分钟内有效）。热线4008206998",msg);
                log.info("获取到的短信:"+msg);
                return msg;
            }
        }
        return  MessageFormat.format("ERROR:尚未收到短信，请确保您已经向手机号{0}发送过短信",phoneNo);
    }


    public static String GetPhoneNoLoop(int count)  throws InterruptedException{
       String phoneNo="";
       int i=0;
        while ((phoneNo==""||phoneNo==null)&&i<count){
            phoneNo=  GetPhoneNo();
            log.info("循环获取手机号码,当前为第"+(i+1)+"次获取");
            i=i+1;
            Thread.sleep(300);
        }
        log.info("跳出循环获取手机号码为:"+phoneNo);
        return phoneNo;
    }


    //1=成功 2=失败
    public static String ReturnState(String phoneNo,String state){
        if (token==null || token.isEmpty())
        {
            Login();
        }
        String url =MessageFormat.format("http://st.jiuapi.cn/api/home/get/reslut?token={0}&phone={1}&state={2}",token,phoneNo,state);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        boolean result = jsonObject.getBoolean("state");
        String msg = jsonObject.getString("message");
        return "回馈注册结果,注册结果为："+state;
    }

    public static void GetUserInfo(){
        if (token==null || token.isEmpty())
        {
            Login();
        }

        String url =MessageFormat.format("http://st.jiuapi.cn/api/home/get/summary?token={0}",token);
        String json = HttpUtils.sendGet(url,null);
        JSONObject jsonObject = (JSONObject)JSONObject.parse(json);
        boolean result = jsonObject.getBoolean("state");
        String msg = jsonObject.getString("message");
        log.info("账号信息为:"+msg);
    }




    public static void main(String[] args) throws InterruptedException {

        //GetPhoneNoLoop(100);
//        Login();

//        String phoneNo="";
//        while (phoneNo==""||phoneNo==null){
//            phoneNo=  GetPhoneNo();
//            log.info("账号信息为:"+phoneNo);
//            Thread.sleep(5000);
//        }
//        log.info("账号信息为:"+phoneNo);

//        String msg="ERROR";
//        while (msg.startsWith("ERROR")){
//            msg=  GetMessage("13191683997");
//            log.info("msg:"+msg);
//            Thread.sleep(5000);
//        }


        ReturnState("17839326168","2");
        ReturnState("15240889121","2");
        ReturnState("13757247568","2");
        ReturnState("17839659197","2");
        ReturnState("18086422610","2");


//
//        GetUserInfo();
    }
}
